package cn.zcy.test;

import cn.zcy.domain.Account;
import cn.zcy.service.AccountService;
import config.SpringConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class SpringTestTx {
    @Autowired
//    @Qualifier(value = "proxyAccountService")
    private AccountService accountService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    DataSource source;

    @Test
    public void dataSourcesTest() throws Exception {
        Connection connection = source.getConnection();
//        connection.setAutoCommit(false);
        boolean autoCommit = connection.getAutoCommit();
        System.out.println("autoCommit = " + autoCommit);

    }

    @Test
    public void saveOne() {

        jdbcTemplate.update("insert into account (username,money) values (?,?)", "王五", 1000);
    }

    @Test
    public void findOne() {
        List<Account> accountList = jdbcTemplate.query("select * from account where id=?", new BeanPropertyRowMapper<Account>(Account.class), 1);
        accountList.forEach((t) -> {
            System.out.println("t = " + t);
        });
        Map<String, Object> map = new HashMap<>();
        Set<String> strings = map.keySet();

        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> m : map.entrySet()) {

        }
    }

    @Test
    public void testSave() {
        Account account = new Account();
        account.setId(6);
        account.setUsername("张三1");
        account.setMoney(1000d);
        accountService.save(account);
    }

    @Test
    public void testSave1() {
        Account account = new Account();
        account.setId(2);
        account.setUsername("李三");
        account.setMoney(1000d);
        accountService.save(account);
    }

    @Test
    public void update() {
        Account account = new Account();
        account.setId(2);
        account.setUsername("李四");
        account.setMoney(1000d);
        accountService.update(account);
    }

    @Test
    public void testFindById() {
        Account accountById = accountService.findAccountById(2);
        System.out.println("accountById = " + accountById);
    }

    /**
     * 查询全部
     */
    @Test
    public void findAll() {
        List<Account> list = accountService.findAll();
        for (Account account : list) {
            System.out.println("account = " + account);
        }

    }

    @Test
    public void transfer() {
        accountService.transfer("张三", "李四", 100.00);
    }


}
